﻿//-----------------------------------------------------------------------
// <copyright file="AuthorizationServiceBase.Tasks.cs" company="Alan Rodrigues">
//     Copyright (c) Alan Rodrigues. All rights reserved.
// </copyright>
// <developed>Alan Rodrigues (operninha@gmail.com)</developed>
//-----------------------------------------------------------------------

namespace Authorization
{

    using Properties;

    public partial class AuthorizationServiceBase
    {
        #region Tasks
        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="scopeName"></param>
        /// <returns></returns>
        public TaskCollection GetAllTasks(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            Application application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.GetTasks", Resources.invalid_application);
            }
            return application.GetTasks(scopeName);
        }

        public string[] GetAllTaskNames(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.GetAllTaskNames", Resources.invalid_application);
            }
            return application.GetTaskNames(scopeName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="scopeName"></param>
        /// <param name="taskName"></param>
        /// <returns></returns>
        public Task GetTask(string applicationName, string scopeName, string taskName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            Application app = AuthorizationStoreManager.GetApplication(applicationName);
            if (app == null)
            {
                throw new AuthorizationException("AuthorizationService.GetTask", Resources.invalid_application);
            }
            var task = app.GetTask(taskName, scopeName);
            if (task == null)
                throw new AuthorizationException("Authorization.GetTasksInTask", string.Format(Resources.task_not_found, taskName));

            return task;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationName"></param>
        /// <param name="taskName"></param>
        /// <param name="description"></param>
        /// <param name="scopeName"></param>
        /// <returns></returns>
        public Task CreateTask(string applicationName, string scopeName, string taskName, string description)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.CreateTask", Resources.invalid_application);
            }

            var task = application.CreateTask(taskName, description, false, scopeName);
            task.Save();
            return task;
        }

        public void DeleteTask(string applicationName, string scopeName, string taskName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref taskName, true, true, false, 64, "taskName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.DeleteTask", Resources.invalid_application);
            }

            application.DeleteTask(taskName, scopeName);
            application.Save();
        }

        public void UpdateTask(string applicationName, string scopeName, string taskName, string newTaskName, string newDescription)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref taskName, true, true, false, 64, "taskName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.UpdateTask", Resources.invalid_application);
            }

            var task = application.UpdateTask(taskName, scopeName, newTaskName, newDescription);
            task.Save();
        }

        public bool TaskExists(string applicationName, string scopeName, string taskName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref taskName, true, true, false, 64, "taskName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.TaskExists", Resources.invalid_application);
            }

           return application.TaskExists(taskName, scopeName);

        }

        public bool IsUserInTask(string userName, string applicationName, string taskName, string[] scopeNames)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref taskName, true, true, false, 64, "taskName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");
            return UserAuthorizarionServiceBase.IsUserInTask(userName, applicationName, taskName, scopeNames);
        }


        public string[] GetTaskNamesInTask(string applicationName, string scopeName, string taskName)
        {
            return  GetTask(applicationName, scopeName, taskName).GetTaskNames();
        }

        public TaskCollection GetTasksInTask(string applicationName, string scopeName, string taskName)
        {
            return GetTask(applicationName, scopeName, taskName).GetTasks();
        }

        public string[] GetOperationNamesInTask(string applicationName, string scopeName, string taskName)
        {
            return GetTask(applicationName, scopeName, taskName).GetOperationNames();
        }

        public OperationCollection  GetOperationsInTask(string applicationName, string scopeName, string taskName)
        {
            return GetTask(applicationName, scopeName, taskName).GetOperations();
        }

        public void AddOperationToTask(string applicationName, string scopeName, string taskName, string operationName)
        {
            SecUtility.CheckParameter(ref operationName, true, true, false, 64, "operationName");

            var task = GetTask(applicationName, scopeName, taskName);
            if (task != null)
            {
                task.AddOperation(operationName);
                task.Save();
            }
            else
                throw new AuthorizationException("AuthorizationService.AddOperationToTask", Resources.task_not_found);
        }

        public void RemoveOperationFromTask(string applicationName, string scopeName, string  taskName, string  operationName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref taskName, true, true, false, 64, "taskName");

            var task = GetTask(applicationName, scopeName, taskName);

            if (task != null)
            {
                task.RemoveOperation(operationName);
                task.Save();
            }
        }

        public void RemoveTaskFromTask(string applicationName, string scopeName, string parentTaskName, string childTaskName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref parentTaskName, true, true, false, 64, "parentTaskName");
            SecUtility.CheckParameter(ref childTaskName, true, true, false, 64, "childTaskName");

            var task = GetTask(applicationName, scopeName, parentTaskName);
            
            if (task != null)
            {
                task.RemoveTask(childTaskName);
                task.Save();
            }
        }

        public void AddTaskToTask(string applicationName, string parentScopeName,  string parentTaskName, string childTaskName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref parentTaskName, true, true, false, 64, "parentTaskName");
            SecUtility.CheckParameter(ref childTaskName, true, true, false, 64, "childTaskName");

            var task = GetTask(applicationName, parentScopeName, parentTaskName);
            if (task != null)
            {
                task.AddTask(childTaskName);
                task.Save();
            }
        }
       
        #endregion
    }
}
